package com.ruoyi.quanzi.controller.app;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quanzi.domain.SunComment;
import com.ruoyi.quanzi.service.ISunCommentService;

/**
 * 帖子评论接口
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/app/post")
public class AppCommentController extends BaseController
{
    @Autowired
    private ISunCommentService commentService;

    /**
     * 查询评论列表
     */
    @GetMapping("/comments")
    public TableDataInfo listComments(SunComment comment)
    {
        startPage();
        List<SunComment> list = commentService.selectSunCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 查询帖子评论列表
     */
    @GetMapping("/{postId}/comments")
    public TableDataInfo list(@PathVariable("postId") Long postId)
    {
        startPage();
        List<SunComment> list = commentService.selectCommentsByPostId(postId);
        return getDataTable(list);
    }

    /**
     * 新增帖子评论
     */
    @PostMapping("/{postId}/comment")
    public AjaxResult add(@PathVariable("postId") Long postId, @RequestBody SunComment comment)
    {
        comment.setPostId(postId);
        comment.setUid(SecurityUtils.getUserId());
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 删除帖子评论
     */
    @DeleteMapping("/comment/{commentId}")
    public AjaxResult remove(@PathVariable Long commentId)
    {
        // 验证当前用户是否有权限删除该评论
        SunComment comment = commentService.selectCommentById(commentId);
        if (comment != null && !comment.getUid().equals(SecurityUtils.getUserId()))
        {
            return AjaxResult.error("无权删除他人评论");
        }
        return toAjax(commentService.deleteCommentById(commentId));
    }
}